/* GUI CONVERTED from resqwba.p (converter v1.76) Mon Aug 12 22:34:42 2002 */
/* resqwba.p - Line Sequence WorkBench-subprogram                             */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.12 $                                                          */
/*V8:ConvertMode=Maintenance                                                  */
/* REVISION: 7.0    LAST MODIFIED: 12/04/91     BY: smm *F230*                */
/* REVISION: 7.0    LAST MODIFIED: 04/04/92     BY: smm *F364*                */
/* REVISION: 7.0    LAST MODIFIED: 05/16/92     BY: emb *F531*                */
/* REVISION: 7.3    LAST MODIFIED: 12/10/92     BY: emb *G468*                */
/* REVISION: 7.3    LAST MODIFIED: 01/07/93     BY: emb *G524*                */
/* REVISION: 7.3    LAST MODIFIED: 09/15/94     by: slm *GM63*                */
/* REVISION: 7.3    LAST MODIFIED: 10/05/94     by: qzl *GN09*                */
/* REVISION: 7.3    LAST MODIFIED: 10/05/94     by: qzl *F0CR*                */
/* REVISION: 7.2    LAST MODIFIED: 04/10/95     by: ais *F0Q2*                */
/* REVISION: 7.2    LAST MODIFIED: 06/09/95     by: qzl *G0PV*                */
/* REVISION: 7.3    LAST MODIFIED: 07/18/95     by: dzs *G0SB*                */
/* REVISION: 7.3    LAST MODIFIED: 12/04/95     by: jym *F0WF*                */
/* REVISION: 7.3    LAST MODIFIED: 01/30/96     by: jym *G1LJ*                */
/* REVISION: 7.3    LAST MODIFIED: 02/05/96     by: jym *G1M8*                */
/* REVISION: 7.3    LAST MODIFIED: 11/11/96     BY: *G2HW* Julie Milligan     */
/* REVISION: 8.6    LAST MODIFIED: 05/20/98     BY: *K1Q4* Alfred Tan         */
/* REVISION: 9.0    LAST MODIFIED: 01/29/98     BY: *M066* John Pison         */
/* REVISION: 9.0    LAST MODIFIED: 03/13/99     BY: *M0BD* Alfred Tan         */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00     BY: *N0KP* myb                */
/* Old ECO marker removed, but no ECO header exists *F0PN*                    */
/* Revision: 1.9    BY: Rajesh Thomas           DATE: 05/01/01 ECO: *M15B*    */
/* Revision: 1.10   BY: Saurabh Chaturvedi      DATE: 04/23/02 ECO: *M1XV*    */
/* $Revision: 1.12 $ BY: Amit Chaturvedi    DATE: 08/06/02 ECO: *N1P3*     */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{mfdeclre.i} /* SHARED VARIABLE INCLUDE */

/* IF YES THEN WILL SHOW ERROR MESSAGES TO USER */

define input parameter p-show-messages    as logical no-undo.

define new shared variable shopcal_recno  as   recid.
define new shared variable use_detail     like mfc_logical.
define new shared variable done_division  like mfc_logical.
define new shared variable amt_seq        like seq_qty_req.
define new shared variable qtyreq         like seq_qty_req.
define new shared variable temp_rel       like seq_due_date.
define new shared variable line_rate      like lnd_rate.
define new shared variable part           like seq_part.
define new shared variable leftover       like mfc_logical.
define new shared variable undo-input     like mfc_logical.
define new shared variable lnd_recno      as   recid.
define new shared variable sequence       as   decimal initial 0.
define new shared variable used_hours     as   decimal.
define new shared variable shft_amt       as   decimal decimals 2 extent 4.
define new shared variable hours          as   decimal extent 4.
define new shared variable cap            as   decimal extent 4.
define     shared variable undo-input1    like mfc_logical no-undo.
define     shared variable multiple       as   decimal.
define     shared variable site           like seq_site.
define     shared variable prline         like seq_line.
define     shared variable begin_date     like seq_due_date.
define     shared variable lundo-input2   like mfc_logical no-undo.

define            variable gross_qty      like seq_qty_req        no-undo.
define            variable temp_date      like seq_start          no-undo.
define            variable i              as   integer            no-undo.
define            variable j              as   integer            no-undo.
define            variable old_part       like seq_part           no-undo.
define            variable increment      as   integer            no-undo.
define            variable found_date     like mfc_logical        no-undo.
define            variable ii             as   integer            no-undo.
define            variable l_count        as   integer initial 1  no-undo.
define            variable start_priority as   decimal            no-undo.
define            variable new_priority   as   decimal            no-undo.
define            variable changetime     like chg_time           no-undo.
define            variable check_date     as   date               no-undo.
define            variable yn             like mfc_logical        no-undo.

define workfile work_seq no-undo
   field work_part like seq_part
   field work_qty  like seq_qty_req.

define buffer seq for  seq_mstr.

ii = 0.

for each seq_mstr
   fields(seq_due_date seq_line   seq_part   seq_priority seq_qty_req
          seq_shift1   seq_shift2 seq_shift3 seq_shift4   seq_site)
   where seq_site = site
     and seq_line = prline
     and seq_due_date >= begin_date
   no-lock
   use-index seq_sequence:

   assign
      start_priority = seq_priority
      temp_rel       = begin_date
      old_part       = seq_part
      new_priority   = max(truncate(start_priority + .9999999999,0),1)
      ii             = 1.
   leave.
end. /* FOR EACH seq_mstr */

if ii = 0 then leave.

if temp_rel = ?
then
   temp_rel = begin_date.

{recaldt.i temp_rel shopcal_recno}

/* GET CAPACITY FOR NEW DAY */
{recalcap.i &date=temp_rel}

if cap[1] = 0
and cap[2] = 0
and cap[3] = 0
and cap[4] = 0
then do:

   repeat:
/*GUI*/ if global-beam-me-up then undo, leave.

      if p-show-messages = yes
      then do:

         hide message no-pause.
         {pxmsg.i &MSGNUM=403 &ERRORLEVEL=4}
         /* ZERO CAPACITY DEFINED FOR SCHEDULING */
         hide message.
      end. /* IF p-show-messages = yes */
      leave.
   end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* REPEAT */
   leave.
end. /* IF cap[1] = 0 ... */
if temp_rel - check_date > 7
then do:

   if p-show-messages = yes
   then do:

      hide message no-pause.
      {pxmsg.i &MSGNUM=404 &ERRORLEVEL=2
               &MSGARG1=temp_rel - check_date}
      {pxmsg.i &MSGNUM=8500 &ERRORLEVEL=1 &CONFIRM=yn
               &CONFIRM-TYPE='LOGICAL'}
      if yn = no
      then
         leave.
   end. /*IF p-show-messages = yes */

end. /* IF temp_rel - check_date > 7 */

for each seq_mstr
   fields(seq_due_date seq_line   seq_part   seq_priority seq_qty_req
          seq_shift1   seq_shift2 seq_shift3 seq_shift4   seq_site)
   where seq_site     =  site
     and seq_line     =  prline
     and seq_priority >= start_priority
   no-lock
   break by seq_site
         by seq_line
         by seq_priority:

   if old_part = ""
   then
      old_part = seq_part.

   if seq_part <> old_part
   then do:

      create work_seq.
      assign
         work_part = old_part
         work_qty  = gross_qty
         gross_qty = 0.
   end. /* IF seq_part <> old_part */
   assign
      gross_qty = gross_qty + seq_qty_req
      old_part  = seq_mstr.seq_part.

   if last (seq_site)
   then do:

      create work_seq.
      assign
         work_part = old_part
         work_qty  = gross_qty.
   end. /* IF LAST (seq_site) */

end. /* FOR EACH seq_mstr */

undo-loop:
do on error undo, leave:
/*GUI*/ if global-beam-me-up then undo, leave.


   for each seq_mstr exclusive-lock
      where seq_site     =  site
        and seq_line     =  prline
        and seq_priority >= start_priority:

      assign
         seq_shift1   = 0
         seq_shift2   = 0
         seq_shift3   = 0
         seq_shift4   = 0
         seq_qty_req  = 0
         seq_priority = - seq_priority
         seq_due_date = seq_due_date - 182500.
   end. /* FOR EACH seq_mstr */

   work-loop:
   for each work_seq:

      qtyreq = work_qty.

      /* CONSIDERS 0 QTY ALSO TO DISPLAY CHANGE-OVER ON REPORT */
      if qtyreq >= 0
      and work_part <> part
      then do:

         for first chg_mstr
            fields(chg_from chg_line chg_site chg_time chg_to)
            where chg_site = site
              and chg_line = prline
              and chg_from = part
              and chg_to   = work_part
            no-lock:
         end. /* FOR FIRST chg_mstr */

         if not available chg_mstr
         then
            for first chg_mstr
               fields(chg_from chg_line chg_site chg_time chg_to)
               where chg_site = site
                 and chg_line = prline
                 and chg_from = ""
                 and chg_to   = work_part
               no-lock:
         end. /* FOR FIRST chg_mstr */

         if available chg_mstr
         then do:

            changetime = chg_time.

            do while changetime > 0:

               if cap[1] + cap[2] + cap[3] + cap[4] > 0
               then
               do ii = 1 to 4:
                  if hours[ii] > changetime
                  then
                     assign
                        cap[ii]    = cap[ii] * (1 - changetime / hours[ii])
                        /* ROUNDING TO TWO DECIMAL PLACES */
                        cap[ii]    = round (cap[ii], 2)
                        hours[ii]  = hours[ii] - changetime
                        changetime = 0.
                  else
                  assign
                     changetime = changetime - hours[ii]
                     hours[ii]  = 0
                     cap[ii]    = 0.

                  if ii = 4
                  and changetime > 0
                  then
                     temp_rel = temp_rel + 1.

               end. /* IF cap[1] + cap[2] + cap[3] + cap[4] > 0 */

               if cap[1] = 0
               and cap[2] = 0
               and cap[3] = 0
               and cap[4] = 0
               then do:

                  if changetime = 0
                  then
                     temp_rel = temp_rel + 1.

                  {recaldt.i temp_rel shopcal_recno}
                  /* Get capacity for new day */
                  {recalcap.i &date=temp_rel}
               end. /* IF cap[1] = 0 */
            end. /* DO WHILE changetime > 0 */
         end. /* IF AVAILABLE chg_mstr */
      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* IF qtyreq >= 0 AND work_part <> part */

      /* CONSUME CAPACITY OF WORK_FILE REQUIREMENT */

      repeat while qtyreq > 0:
/*GUI*/ if global-beam-me-up then undo, leave.

         assign
            part    = work_part
            amt_seq = 0.

         {gprun.i ""resqmta.p"" "(input p-show-messages)"}
/*GUI*/ if global-beam-me-up then undo, leave.


         if undo-input
         then
            undo undo-loop, leave.

         if undo-input
         then
            leave.
         undo-input1 = no.

         if amt_seq <> 0
         then do:

            find seq_mstr exclusive-lock
               where seq_site     = site
                 and seq_line     = prline
                 and seq_part     = work_part
                 and seq_due_date = temp_rel
               no-error.
            if not available seq_mstr
            then
               find first seq_mstr exclusive-lock
                  where seq_site     = site
                    and seq_line     = prline
                    and seq_part     = work_part
                    and seq_due_date < low_date
               no-error.
            if not available seq_mstr
            then do:

               create seq_mstr.
               assign
                  seq_part     = work_part
                  seq_site     = site
                  seq_line     = prline
                  seq_due_date = temp_rel.
               recno = recid(seq_mstr).
            end. /* IF NOT AVAILABLE seq_mstr */

            assign
               seq_qty_req  = seq_qty_req + amt_seq
               seq_due_date = temp_rel
               seq_shift1   = shft_amt[1] + seq_shift1
               seq_shift2   = shft_amt[2] + seq_shift2
               seq_shift3   = shft_amt[3] + seq_shift3
               seq_shift4   = shft_amt[4] + seq_shift4.

            if seq_priority = ?
            or seq_priority < 0
            or seq_priority = 0
            then
               assign
               seq_priority = new_priority
               new_priority = new_priority + 1.
         end. /* IF amt_seq <> 0 */

         /* IF UNABLE TO SCHEDULE QTY FOR 1000 DAYS (3 YEARS), THEN DISPLAY */
         /* ERROR #4631 AND RETURN CONTROL TO FIRST FRAME IN resqwb.p.      */

         else do:

            l_count = l_count  + 1.
            if l_count > 1000
            then do:

               hide message no-pause.

               /* INSUFFICIENT CAPACITY TO SCHEDULE MULTIPLE QUANTITY */
               {pxmsg.i &MSGNUM=4631 &ERRORLEVEL=4}
               lundo-input2 = yes.
               leave undo-loop.
            end. /* IF l_count > 1000 ... */
         end. /* IF amt_seq = 0 ... */

         if cap[1] = 0
         and cap[2] = 0
         and cap[3] = 0
         and cap[4] = 0
         then
            temp_rel = temp_rel + 1.

      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* REPEAT */

      delete work_seq.
   end. /* FOR EACH work_seq */

   for each seq_mstr exclusive-lock
      where seq_site     = site
        and seq_line     = prline
        and seq_priority <= 0:
      delete seq_mstr.
   end. /* FOR EACH seq_mstr */

end. /* UNDO-LOOP */
